package android.slc.appbase.api.slc.callback.pure;

import android.slc.or.SlcCallbackConfig;

import org.jetbrains.annotations.NotNull;

import java.util.concurrent.atomic.AtomicReference;

import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
import io.reactivex.rxjava3.internal.util.EndConsumerHelper;

public abstract class SlcDisposableObserverPure<T> extends SlcObserverPure<T> implements Disposable {

    final AtomicReference<Disposable> upstream = new AtomicReference<>();

    public SlcDisposableObserverPure() {
        super();
    }

    public SlcDisposableObserverPure(SlcCallbackConfig slcCallbackConfig) {
        super(slcCallbackConfig);
    }

    @Override
    public void onSubscribe(@NotNull Disposable d) {
        if (EndConsumerHelper.setOnce(this.upstream, d, getClass())) {
            super.onSubscribe(d);
        }
    }

    @Override
    public final boolean isDisposed() {
        return upstream.get() == DisposableHelper.DISPOSED;
    }

    @Override
    public final void dispose() {
        DisposableHelper.dispose(upstream);
    }
}
